<?php
namespace app\admin\model;

use think\Model;

class Order extends Model{

    public function getStatusAttr($value){
        $config = \think\facade\Config::get('param.param_status');
        return $config[$value];
    }

    public function getAddTimeAttr($value){
        return date('Y-n-j H:i:s',$value);
    }

    //搜索器
    public  function searchOrderSnAttr($query,$value)
    {
        if (!''==$value){
            $query->where('ecm_order.order_sn','like','%'.$value.'%');
        }
    }

    public  function searchStatusAttr($query,$value)
    {
        if (!''==$value){
            $query->where('ecm_order.status',$value);
        }
    }

    public function searchMaxTimeAttr($query,$value)
    {
        if (!empty($value)){
            $query->whereTime('add_time','>=',$value);
        }
    }
    public function searchMinTimeAttr($query,$value)
    {
        if (!empty($value)){
            $query->whereTime('add_time','<=',$value);
        }
    }
    public function searchMaxAttr($query,$value)
    {
        if (!empty($value)){
            $query->where('order_amount','<=',$value);
        }
    }
    public function searchMinAttr($query,$value)
    {
        if (!empty($value)){
            $query->where('order_amount','>=',$value);
        }
    }

    public static function getOrderList($data=[],$query=[],$page = true)
    {
        $model = self::field("ecm_order.*,ecm_order_status.*")
            ->withSearch(array_keys($data),$data)
            ->join('ecm_order_status','ecm_order_status.order_status=ecm_order.status','LEFT')
            ->order('ecm_order.order_id');
//        if(isset($data['cid']) && $data['cid']!=""){
//            $model->where('o.status',$data['cid']);
//        }
//        if(isset($data['smin']) && $data['smin']!=""){
//            $model->where('o.add_time','>=',$data['smin']);
//        }
//        if(isset($data['smax']) && $data['smax']!=""){
//            $model->where('o.add_time','<=',$data['smax']);
//        }
//        if(isset($data['min']) && $data['min']!=""){
//            $model->where('o.goods_amount','>=',$data['min']);
//        }
//        if(isset($data['max']) && $data['max']!=""){
//            $model->where('o.goods_amount','<=',$data['max']);
//        }
//        if(isset($data['keywords']) && $data['keywords']!=""){
//            $model->whereLike('o.order_sn','%'.$data['keywords'].'%');
//        }
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }
    }
}